
using System.Linq;
using System.Web.Mvc;
using Ezio.Models;

namespace Ezio.Controllers
{
    public class OrdenesComprasController : Controller
    {
        DataProvider Provider = new DataProvider();
        
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Agregar(OrdenesCompras OrdenesCompra)
        {
            Provider.AddToOrdenesCompras(OrdenesCompra);
            return RedirectToAction("Index", new { Success = Provider.TrySaveChanges() });
        }
        public ActionResult Index(bool? Success)
        {
            string Message = Success.HasValue ? Success.Value ? "" : "Ha ocurrido un error al procesar su peticion" : "";
            
            ViewData["OrdenesCompras"] = Provider.OrdenesCompras.ToList();
            ViewData["Message"] = Message;
            return View("Index");
        }
        public ActionResult Editar(int Id)
        {
            ViewData["OrdenesCompra"] = Provider.OrdenesCompras.Single(t => t.IdOrdenCompra == Id);
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public bool Update(OrdenesCompras OrdenesCompra)
        {
            OrdenesCompras myOrdenesCompra= Provider.OrdenesCompras.Single(t => t.IdOrdenCompra == OrdenesCompra.IdOrdenCompra);
            myOrdenesCompra.Copy(OrdenesCompra);
            return Provider.TrySaveChanges();
        }

        public ActionResult Eliminar(int Id)
        {
            Provider.OrdenesCompras.DeleteObject(Provider.OrdenesCompras.Single(t => t.IdOrdenCompra == Id));
            return RedirectToAction("Index", new { Success = Provider.TrySaveChanges() }); ;
        }

    }
}
